<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>正则表达式练习</title>
</head>
<body>
    <script>
        //验证用户名的正则表达式：（字母、数字、下划线组成，数字不能开头，长度6到18位）
        let regUsername = /^[a-zA-Z_]\w{5,17}$/
        let username1 = 'tom'
        let username2 = '2tomjack'
        let username3 = 'tom_jack_2'
        console.log(regUsername.test(username1));
        console.log(regUsername.test(username2));
        console.log(regUsername.test(username3));
        console.log('----------------------------------');
        //验证电话号码的正则表达式：（xxx-xxxxxxxx 或 xxxx-xxxxxxxx）
        let regPhone = /^\d{3,4}-\d{8}$/
        let phone1 = '12-12341234'
        let phone2 = '123-12345678'
        let phone3 = '1234-12345678'
        let phone4 = '123a-12345678'
        console.log(regPhone.test(phone1));
        console.log(regPhone.test(phone2));
        console.log(regPhone.test(phone3));
        console.log(regPhone.test(phone4));
        console.log('----------------------------------');
        //验证电话号码的正则表达式：(1[3456789]XXXXXXXXX)
        let telPhoneReg = /^1[3-9]\d{9}$/
        let tel1 = '15912345678'
        let tel2 = '12912345678'
        console.log(telPhoneReg.test(tel1));
        console.log(telPhoneReg.test(tel2));
        console.log('----------------------------------');
        //验证年龄：(0-120)
        let ageReg = /^\d{1,2}$|^1[0-1]\d{1}$|^120$/
        let age1 = 6
        let age2 = 66
        let age3 = 116
        let age4 = 120
        let age5 = 121
        console.log(ageReg.test(age1));
        console.log(ageReg.test(age2));
        console.log(ageReg.test(age3));
        console.log(ageReg.test(age4));
        console.log(ageReg.test(age5));
        console.log('----------------------------------');
        //验证日期：(xxxx-x-x 或者 xxxx-xx-xx)
        let dateReg = /^\d{4}-\d{1,2}-\d{1,2}$/
        let date1 = '2020-11-4'
        let date2 = '2020-11-04'
        let date3 = '2020-1-14'
        console.log(dateReg.test(date1));
        console.log(dateReg.test(date2));
        console.log(dateReg.test(date3));
        console.log('----------------------------------');
        //验证邮箱：(xxx@xxx.com 或者 xxx@xxx.com.cn)
        let emailReg = /^\w+@\w+(\.\w+){1,2}$/
        let email1 = 'abc@qq.com'
        let email2 = 'abc@qq.com.cn'
        let email3 = 'abc@.com'
        console.log(emailReg.test(email1));
        console.log(emailReg.test(email2));
        console.log(emailReg.test(email3));
        console.log('----------------------------------');
        //验证姓名：（2-6位的汉字）
        let nameReg = /^[\u4e00-\u9fa5]{2,6}$/
        let name1 = '霍杰'
        let name2 = '熊梦宇'
        let name3 = '东方不败'
        let name4 = 'jack'
        console.log(nameReg.test(name1));
        console.log(nameReg.test(name2));
        console.log(nameReg.test(name3));
        console.log(nameReg.test(name4));
    </script>
</body>
</html>